import Vue from 'vue';

import iView from 'iview';

Vue.use(iView);
import VueBus from 'vue-bus';
Vue.use(VueBus);

import 'iview/dist/styles/iview.css';
import "./styles/main.less";
import "./styles/main.css";
import {createApp} from "./app";

var {app,router,store} =createApp({});
if (window.__INITIAL_STATE__) {
    store.replaceState(window.__INITIAL_STATE__)
    var el=document.querySelector('#_c')
    el.parentNode.removeChild(el);
}
Vue.mixin({
    // beforeMount () {
    //   const { asyncData } = this.$options
    //   if (asyncData) {
      	
    //     // 将获取数据操作分配给 promise
    //     // 以便在组件中，我们可以在数据准备就绪后
    //     // 通过运行 `this.dataPromise.then(...)` 来执行其他任务
    //     this.dataPromise = asyncData({
    //       store: this.$store,
    //       route: this.$route
    //     })
    //   }
    // },
    // beforeRouteUpdate (to, from, next) {
    //   const { asyncData } = this.$options
    //   if (asyncData) {
    //     asyncData({
    //       store: this.$store,
    //       route: to
    //     }).then(next).catch(next)
    //   } else {
    //     next()
    //   }
    // }
})
// 这里假定 App.vue 模板中根元素具有 `id="app"`
router.onReady(() => {
    console.log('router ready')
    router.beforeResolve((to, from, next) => {
        if (to.matched.some(record => record.meta.requiresAuth)) {
          if (!store.getters.getToken) {
              //iview.Message.info('请登录！');
              return next({
                  path: '/login',
                  query: { redirect: to.fullPath }
              })
          }
        }
        const matched = router.getMatchedComponents(to)
        const prevMatched = router.getMatchedComponents(from)
        let diffed = false
        const activated = matched.filter((c, i) => {
          return diffed || (diffed = (prevMatched[i] !== c))
        })
        const asyncDataHooks = activated.map(c => c.asyncData).filter(_ => _)
        if (!asyncDataHooks.length) {
          return next()
        }
    
        //bar.start()
        Promise.all(asyncDataHooks.map(hook => hook({ store, route: to })))
          .then(() => {
            //bar.finish()
            next()
          })
          .catch(next)
    })
    app.$mount('#app')
})